package com.xhwl.logistics.enums;

import com.xhwl.common.exception.BusinessException;
import com.xhwl.logistics.constant.VehicleConstant;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

@Getter
@AllArgsConstructor
public enum VehicleQuerySortEnum {

    ACCESS_RECORD_SORT_ENTER_TIME("accessRecordEnterTime", "enter_time"),
    ACCESS_RECORD_SORT_ID("accessRecordId", "id");

    private final String sortName;

    private final String sortColumn;

    private static final Map<String, VehicleQuerySortEnum> map;
    static {
        map = new HashMap<>();
        for (VehicleQuerySortEnum vehicleQuerySortEnum : VehicleQuerySortEnum.values()) {
            map.put(vehicleQuerySortEnum.sortName, vehicleQuerySortEnum);
        }
    }

    public static String toSortColumn(String name) {
        return Optional.ofNullable(map.get(name))
                .map(vehicleQuerySortEnum -> vehicleQuerySortEnum.sortColumn)
                .orElseThrow(() -> new BusinessException("非法的排序名称"));
    }

    public static String toDirection(String name) {
        if (VehicleConstant.SORT_DIRECTION_ASC.equals(name)) {
            return VehicleConstant.SORT_DIRECTION_ASC;
        }
        else if (VehicleConstant.SORT_DIRECTION_DESC.equals(name)) {
            return VehicleConstant.SORT_DIRECTION_DESC;
        } else {
            throw new BusinessException("非法的排序方向");
        }
    }

}
